<template>
	<view>
		<view class="result-card">
			<view class="result-header">
				<view class="result-title-section">
					<text class="card-title">优化方案</text>
					<view class="result-stats">
						<view class="stat-item">
							<uni-icons type="bars" size="20" color="#4A90E2"></uni-icons>
							<text class="stat-value">{{ results.length }}根</text>
						</view>
						<view class="stat-item">
							<uni-icons type="star-filled" size="20" color="#67C23A"></uni-icons>
							<text class="stat-value">{{ utilization }}%</text>
						</view>
					</view>
				</view>
			</view>
			<view class="result-panel">
				<scroll-view class="result-list" scroll-y>
					<view v-for="(result, index) in results" :key="index" class="result-item">
						<view class="result-item-header">
							<view class="item-header-info">
								<view class="material-info">
									<text class="result-item-title">原料 {{ result.length }}mm</text>
									<text class="remain-info">余料：{{ result.remaining }}mm</text>
									<view class="header-tags">
										<text class="parts-tag">{{ result.count }}段</text>
									</view>
								</view>
							</view>
						</view>
						<view class="cutting-diagram">
							<view class="parts-grid">
								<view v-for="(part, pIndex) in result.parts" :key="pIndex" class="part-item">
									<view class="part-color-block"
										:style="{ backgroundColor: partColors[pIndex % partColors.length] }"></view>
									<text class="part-length">{{ part.length }}*{{ part.count }}</text>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			results: [],
			utilization: 0,
			partColors: ['#4A90E2', '#67C23A', '#E6A23C', '#F56C6C', '#909399']
		};
	},
	onShareTimeline() {
		return {
			title: '免费计算最优下料方案',
			path: '/pages/index/index',
			imageUrl: '/static/share.jpg',
			type: '0'
		};
	},
	onShareAppMessage() {
		return {
			title: '免费计算最优下料方案',
			path: '/pages/index/index',
			imageUrl: '/static/share.jpg',
			type: '0'
		};
	},
	onLoad(param) {
		let res = uni.getStorageSync("optimisation") || [];
		if (typeof res === "string") {
			try {
				res = JSON.parse(res);
			} catch (e) {
				res = [];
			}
		}

		const reversed = [...res].reverse();
		const data = reversed[param.index];

		if (data) {
			this.results = data.data;
			this.utilization = parseFloat(data.utilization) || 0;
		}
	}
};
</script>

<style>

	.result-card {
		grid-column: 1 / -1;
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 32rpx;
		border: 2rpx solid #f0f0f0;
	}

	.result-header {
		margin-bottom: 32rpx;
		padding-bottom: 24rpx;
		border-bottom: 2rpx solid #F0F2F5;
	}

	.result-title-section {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 16rpx;
	}

	.result-stats {
		display: flex;
		align-items: center;
		gap: 24rpx;
	}

	.stat-item {
		display: flex;
		align-items: center;
		gap: 8rpx;
		background-color: #F5F7FA;
		padding: 8rpx 16rpx;
		border-radius: 8rpx;
	}

	.stat-value {
		font-size: 14px;
		color: #1F2937;
		font-weight: 500;
	}

	.result-status {
		display: inline-flex;
		align-items: center;
		gap: 6rpx;
		background-color: #F0F9EB;
		padding: 6rpx 16rpx;
		border-radius: 32rpx;
	}

	.status-text {
		font-size: 12px;
		color: #67C23A;
	}

	.result-panel {
		display: flex;
		flex-direction: column;
		gap: 24rpx;
		white-space: nowrap;
	}

	.result-list {
		overflow: auto;
		padding: 8rpx;
		margin: -8rpx;
	}

	.result-item {
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 24rpx;
		margin-bottom: 16rpx;
		border: 2rpx solid #f0f0f0;
	}

	.result-item:last-child {
		margin-bottom: 0;
	}

	.result-item-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 24rpx;
	}

	.item-header-info {
		display: flex;
		flex-direction: column;
		gap: 12rpx;
	}

	.material-info {
		display: flex;
		align-items: center;
		gap: 24rpx;
	}

	.remain-info {
		font-size: 14px;
		color: #F56C6C;
	}

	.result-item-title {
		font-size: 16px;
		color: #333333;
		font-weight: 500;
	}

	.header-tags {
		display: flex;
		gap: 12rpx;
	}

	.length-tag,
	.parts-tag {
		font-size: 12px;
		padding: 4rpx 12rpx;
		border-radius: 4rpx;
	}

	.length-tag {
		background-color: #F2F6FC;
		color: #4A90E2;
	}

	.parts-tag {
		background-color: #F0F9EB;
		color: #67C23A;
	}

	.cutting-diagram {
		background-color: #F5F7FA;
		border-radius: 8rpx;
		padding: 24rpx;
		margin-bottom: 16rpx;
	}

	.parts-grid {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(160rpx, 1fr));
		gap: 16rpx;
	}

	.part-item {
		display: flex;
		align-items: center;
		gap: 12rpx;
		background-color: #FFFFFF;
		padding: 16rpx;
		border-radius: 8rpx;
		border: 2rpx solid #f0f0f0;
	}

	.part-color-block {
		width: 24rpx;
		height: 24rpx;
		border-radius: 6rpx;
		flex-shrink: 0;
	}

	.part-length {
		font-size: 14px;
		color: #333333;
		font-weight: 500;
	}

	.cutting-info {
		display: flex;
		align-items: center;
		gap: 8rpx;
	}

	.remain-text {
		font-size: 12px;
		color: #909399;
	}

	.remain-value {
		font-size: 12px;
		color: #F56C6C;
		font-weight: 500;
	}

	.optimize-button {
		margin: 32rpx 24rpx;
		height: 88rpx;
		background: linear-gradient(135deg, #4A90E2 0%, #357ABD 100%);
		color: #FFFFFF;
		font-size: 16px;
		font-weight: 500;
		border-radius: 12rpx;
		text-align: center;
		line-height: 88rpx;
		border: none;
		box-shadow: 0 4rpx 12rpx rgba(74, 144, 226, 0.2);
	}

	.optimize-button:active {
		transform: scale(0.98);
		box-shadow: 0 2rpx 8rpx rgba(74, 144, 226, 0.1);
	}

	/deep/ ::-webkit-scrollbar {
		width: 4rpx;
		background: transparent;
	}

	/deep/ ::-webkit-scrollbar-thumb {
		background-color: rgba(0, 0, 0, 0.1);
		border-radius: 2rpx;
	}
</style>
